<?php

namespace App\Http\Controllers\Api;

use App\Services\CartService;
use Illuminate\Http\Request;


class CartController extends Controller
{

    //整合代码 复用
    protected $cartService;
    // 利用 Laravel 的自动解析功能注入 CartService 类
    public function __construct(CartService $cartService)
    {
        $this->cartService = $cartService;
    }

    //购物车展示页面
    public function index(Request $request)
    {
        $cartItems = $this->cartService->get();
        $addresses = $request->user()->addresses()->orderBy('last_used_at','desc')->get();

        return $this->response->array(['cartItems' => $cartItems,'addresses' => $addresses,]);

    }
}
